<?php
/**
 * 记账（创建应收应付单）
 * Enter description here ...
 * @author nijn
 *
 */
class AccountingBehavior extends CActiveRecordBehavior {
	/**
	 * 订单所对应的状态
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $status = '';
	public $adminId = 1;
	public $adminName = '系统';
	
	public function beforeSave($event) {
		$owner = $this->getOwner();
		$relateId =  $owner->id;
		$obj = Receivables::model()->countByAttributes(array('relate_id'=>$relateId));
		if ($owner->status == $this->status && $obj==0) {
			foreach (array(Receivables::TYPE_RA,Receivables::TYPE_R) as $one) {
				$model = new $one;
				$isVable = $one == Receivables::TYPE_RA;
				$model->attributes = array(
					'relate_id' => $relateId,
					'worker_id' =>  $isVable ? CHtml::value($owner, 'items.provider_id') : $this->adminId,
					'receiver' => $isVable ? CHtml::value($owner, 'items.provider.profile.nickname') : $this->adminName,
					'payer' =>  $isVable ? $this->adminName : CHtml::value($owner, 'member.profile.nickname'),
					'receiveable' => $isVable ? $owner->TotalPrice : '',
					'received' => $isVable ? '' : $owner->TotalPrice
				);
				$model->save();
			}
			
		}
	}
}

?>